/***************************************************************************
 *   Copyright (C) 2006 by Archimedes Projekt   *
 *   thelsb@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#ifndef GSSGLOBALS_H
#define GSSGLOBALS_H
#include <QDataStream>
#include <QString>
//#include "hpsc.h"
#include <iostream>
#include <QMetaType>
#include <QtDebug>

//custom event types
#define HPSCEvent           (QEvent::Type)((int)QEvent::User + 1)
#define HPSCMEAS            (QEvent::Type)((int)QEvent::User + 2)
#define HPSCEventSerial     (QEvent::Type)((int)QEvent::User + 3)
#define HPSCSTRVAL          (QEvent::Type)((int)QEvent::User + 4)

//HACKER PORT INPUT PORT A PIN ASIGNMENT
#define UMBILICAL_CONN        PIN0
#define SERVCON_SEP_CONN      PIN1
#define REXUS_CONN            PIN2
#define SERVICE_MOD_KEY       PIN3
#define FLIGHT_SIM_KEY        PIN4
#define FLIGHT_MODE_KEY       PIN5
#define SWITCH_POS_B          PIN6
#define SWITCH_POS_C          PIN7
////////////////////////////

//HACKER PORT INPUT PORT B PIN ASIGNMENT
#define I_FLIGHT_SIM_SOE      PIN0
#define I_FLIGHT_SIM_LO       PIN1
#define I_FLIGHT_SIM_STAGE    PIN2
#define I_FLIGHT_SIM_NCSEP    PIN3
#define I_FLIGHT_SIM_YOYO_REL PIN4
#define I_FLIGHT_SIM_CHUTE    PIN5
#define POWER_RACK            PIN6
#define CAMCON_CONN           PIN7
////////////////////////////

//HACKER PORT OUTPUT PORT A PIN ASIGNMENT
#define CHAN_1                PIN0
#define CHAN_2                PIN1
#define CHAN_3                PIN2
#define CHAN_4                PIN3
#define BUS_B_SELECT          PIN6
#define BUS_C_SELECT          PIN7
////////////////////////////

//HACKER PORT OUTPUT PORT B PIN ASIGNMENT
#define O_FLIGHT_SIM_SOE      PIN0
#define O_FLIGHT_SIM_LO       PIN1
#define O_FLIGHT_SIM_STAGE    PIN2
#define O_FLIGHT_SIM_NCSEP    PIN3
#define O_FLIGHT_SIM_YOYO_REL PIN4
#define O_FLIGHT_SIM_CHUTE    PIN5
////////////////////////////

typedef enum SRC_ERR { V_ERR=0x01,A_ERR=0x02,POW_ERR=0x04,TEMP_ERR=0x08,ERR_SRC1=0x10,ERR_SRC2=0x20,ERR_SRC3=0x40,ERR_SRC4=0x80 };
typedef	enum SRC { SRC1=1,SRC2,SRC3,SRC4,NOSRC}; 
typedef enum DEV {DEV_HPSC,DEV_HKP};
typedef enum MSGTYPE {MSG_ERR,MSG_READOUT,MSG_READOUT_V,MSG_READOUT_A,SERIAL,HPSCCMD };
typedef enum COMMAND { RST=0,TRG,STB1,STB2,STB3,STB4,SRE,SRE1,SRE2,SRE3,SRE4,
	ESR1,ESR2,ESR3,ESR4,ESE,ESE1,ESE2,ESE3,ESE4,
	CLS,WAI,TST,SETVOLT1,SETVOLT2,SETVOLT3,SETVOLT4,
	GETVOLT1,GETVOLT2,GETVOLT3,GETVOLT4,GETCURR1,GETCURR2,
	GETCURR3,GETCURR4,SETCURR1,SETCURR2,SETCURR3,SETCURR4,BUS1,BUS2,BUS3,BUS4};  
typedef enum MODULE { HPSC,CAMCON,SERVCON,PODCON };

typedef struct {
	COMMAND cmd;
	float var;
	long usWait;

} HPSC_Command;
		
inline QDataStream & operator<<(QDataStream &s,const HPSC_Command &command)
{
	s.writeRawData((char*)&command,sizeof(command));
	return s;
}

inline QDataStream & operator>>(QDataStream &s, const HPSC_Command &command)
{
	s.readRawData((char*)&command,sizeof(command));
	return s;
}

inline QDataStream & operator<<( QDataStream &s,const MSGTYPE &msgType)
{
	if(s.writeRawData((char*)&msgType,sizeof(msgType))==-1)
	std::cout<<"wirting error";	
	return s;
}

inline QDataStream & operator>>( QDataStream &s, const MSGTYPE &msgType)
{
	if(s.readRawData((char*)&msgType,sizeof(msgType))==-1)
	std::cout<<"reading error";	
	return s;
}



/*!
    \fn HPSC_CommandToSTring(HPSC_Command command)
 */
inline QString HPSC_CommandToString(HPSC_Command command)
{
	qDebug()<<"HPSC_CommandToSTring(HPSC_Command command)";
	QString val = QString::number(command.var);
	
	switch(command.cmd)
	{
		case CLS: return QString("*CLS");
		break;
		case RST:  return QString("*RST");
		break;
		case SETVOLT1:  return QString("VOLT1 ")+val;
		break;
		case SETCURR1:  return QString("CURR1 ")+val;
		break;
		case SETVOLT2:  return QString("VOLT2 ")+val;
		break;
		case SETCURR2:  return QString("CURR2 ")+val;
		break;
		case SETVOLT3:  return QString("VOLT3 ")+val;
		break;
		case SETCURR3:  return QString("CURR3 ")+val;
		break;
		case SETVOLT4:  return QString("VOLT4 ")+val;
		break;
		case SETCURR4:  return QString("CURR4 ")+val;
		break;
      case TRG:
      case STB1:
      case STB2:
      case STB3:
      case STB4:
      case SRE:
      case SRE1:
      case SRE2:
      case SRE3:
      case SRE4:
      //case ESR:
      case ESR1:
      case ESR2:
      case ESR3:
      case ESR4:
      case ESE:
      case ESE1:
      case ESE2:
      case ESE3:
      case ESE4:
      case WAI:
      case TST:
      case GETVOLT1:
      case GETVOLT2:
      case GETVOLT3:
      case GETVOLT4:
      case GETCURR1:
      case GETCURR2:
      case GETCURR3:
      case GETCURR4:      
        break;
        default: break;
	}
}

// inline QDataStream & operator<<( QDataStream &s, HPSC::SRC &src)
// {
// 	s.writeRawData((char*)&src,sizeof(src));
// 	return s;
// }
// 
// inline QDataStream & operator>>( QDataStream &s, HPSC::SRC &src)
// {
// 	s.readRawData((char*)&src,sizeof(src));
// 	return s;
// }
Q_DECLARE_METATYPE(HPSC_Command)
#endif
        